package com.shouhuobao.take.invite;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.app.WebViewActivity;
import com.iseastar.guojiang.consts.AppConfig;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.shouhuobao.station.R;
import com.shouhuobao.station.wxapi.WXshare;

import droid.frame.activity.title.TitleRes;
import droid.frame.ui.utils.Utils;

/**
 * 邀请好友开通驿站
 */
public class StationInviteActivity extends BaseActivity2 implements View.OnClickListener {

    private String tipsStr = "<font color=#e8e8e8>马上邀请好友开通驿站得</font><font color=#FDD120>现金</font><font color=#e8e8e8>好礼！</font>";

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_station_invite);
        super.findViewById();
        getAppTitle().setCommonTitle("邀请好友开通驿站", new TitleRes("邀请记录", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(StationInviteHistoryActivity.class);
            }
        }));
        ((TextView) findViewById(R.id.tips_tv)).setText(Html.fromHtml(tipsStr));
        findViewById(R.id.check_activity_tv).setOnClickListener(this);
        findViewById(R.id.invite_bt).setOnClickListener(this);

    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //邀请好友
            case R.id.invite_bt:
                View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_invite_share, null);
                PopupWindow popupWindow = DialogMgr.createPopupWindow(getContext(), view);
                popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
                popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                popupWindow.showAtLocation(findViewById(R.id.invite_bt), Gravity.BOTTOM, 0, Utils.dpToPx(55, getResources()));
                popupWindowView(view, popupWindow);
                break;
            //查看活动规则
            case R.id.check_activity_tv:
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("title", "活动规则");
                startActivity(intent);
                break;
        }
    }

    /**
     * 更新popupwindow状态和显示
     *
     * @param view
     * @param popupWindow
     */
    private void popupWindowView(View view, final PopupWindow popupWindow) {
        final WXshare mWXshare = new WXshare(this);
        final String url = AppConfig.getGuoErServerUrl() + "static/promo/stageInvite.html?userId=" + AppCache.getUser().getId();
        final String title = "快来开通火箭驿站吧!";
        final String description = "现在加入您将获得5重好礼及多多福利";
        final Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.station_open_share);
        //微信好友
        view.findViewById(R.id.wx_friend_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWXshare.shareWX(0, title, description, icon, url, null, null);
                popupWindow.dismiss();
            }
        });
        //微信朋友圈
        view.findViewById(R.id.wx_circule_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWXshare.shareWX(1, title, description, icon, url, null, null);
                popupWindow.dismiss();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
